<?php

require_once ('application/controllers/basecontroller.php');
class scheduling extends BaseController {
	
	function __construct() {
		parent::__construct ();
		$this->load->model('usermodel');
		$this->load->model('schedulingmodel');
		$this->session->set_userdata("navindex",1);
	}
	function _loadAsideView()
	{
		$this->load->view("scheduling/schedulingaside");
	}
	function _loadContentView()
	{
		if ($this->_testuserlogin()<0)
		{
			redirect(base_url("index.php/user/"));	
			return ;
		}
		else if (isset($this->baseControllerData[$this->DATATYPE_ERROR]))
		{
			parent::_loadContentView();
			unset($this->baseControllerData[$this->DATATYPE_ERROR]);
		}
		else if (isset($this->baseControllerData[$this->DATATYPE_CONTENT]))
		{
			if ($this->baseControllerData[$this->DATATYPE_CONTENT]["type"] ==0)
			{
				
			}
			else if ($this->baseControllerData[$this->DATATYPE_CONTENT]["type"] ==1)
			{
				$this->load->view("scheduling/autoschedulingview");
			}
			else if ($this->baseControllerData[$this->DATATYPE_CONTENT]["type"] ==2)
			{
				$this->load->view("scheduling/searchscheduling",$this->baseControllerData[$this->DATATYPE_CONTENT]);
				$this->load->view("scheduling/continuescheduling",$this->baseControllerData[$this->DATATYPE_CONTENT]);
			}
			else if ($this->baseControllerData[$this->DATATYPE_CONTENT]["type"] ==3)
			{
				
				$this->load->view("scheduling/continuescheduling",$this->baseControllerData[$this->DATATYPE_CONTENT]);
			}
			else if ($this->baseControllerData[$this->DATATYPE_CONTENT]["type"] ==4)
			{
				$this->load->view("scheduling/editview",$this->baseControllerData[$this->DATATYPE_CONTENT]);
			}
		}
		else
		{
			$table2Array =$this->_getDate();
			$this->baseControllerData[$this->DATATYPE_CONTENT]["list"]=$table2Array;
			$this->load->view("scheduling/continuescheduling",$this->baseControllerData[$this->DATATYPE_CONTENT]);
			
		}
		//$this->load->view('loginview');
		
	}
	function listscheduling($id=-1)
	{
		$this->baseControllerData[$this->DATATYPE_CONTENT]["type"] =3;
		$table2Array =$this->_getDate(null,null,$id);
		$this->baseControllerData[$this->DATATYPE_CONTENT]["list"]=$table2Array;
		parent::index();
	}
	function autoscheduling()
	{
		$this->baseControllerData[$this->DATATYPE_CONTENT]["type"] =1;
		parent::index();
	}
	function search()
	{
		$this->baseControllerData[$this->DATATYPE_CONTENT]["type"] =2;
		if (isset($_POST["uid"]))
		{
			$table2Array=$this->_getDate($_POST["start_date"],$_POST["end_date"],$_POST["uid"]);
			//less admin
			
			$this->baseControllerData[$this->DATATYPE_CONTENT]["start_date"] =$_POST["start_date"];
			$this->baseControllerData[$this->DATATYPE_CONTENT]["end_date"] =$_POST["end_date"];
			$this->baseControllerData[$this->DATATYPE_CONTENT]["uid"] =$_POST["uid"];
			$this->baseControllerData[$this->DATATYPE_CONTENT]["list"]=$table2Array;

		}
		$userlist = $this->usermodel->getuserbylesstype(4);
		$this->baseControllerData[$this->DATATYPE_CONTENT]["userlist"]=$userlist;
		
		parent::index();
	}
	function storescheduling()
	{
		if (!isset($_POST["start_date"]))
		{
			$this->showerrmsg("start month is not setting");
			return ;
		}
		$leaders = $this->usermodel->getuserbytype(1);
		$staffs = $this->usermodel->getuserbytype(0);
		$tableArray = array();
		$this->baseControllerData[$this->DATATYPE_CONTENT]["type"] =3;
		$this->baseControllerData[$this->DATATYPE_CONTENT]["start_date"] =$_POST["start_date"];
		$this->baseControllerData[$this->DATATYPE_CONTENT]["end_date"] =$_POST["end_date"];
		
		$i =0;
		$j =0;
		$leaderCount = count($leaders);
		$staffCount=count($staffs);
		$startdate =   strtotime($this->baseControllerData[$this->DATATYPE_CONTENT]["start_date"]);
		$enddate = strtotime( $this->baseControllerData[$this->DATATYPE_CONTENT]["end_date"]);
		$tmpDate = $startdate;
		//die(print_r($leaders));
		while($tmpDate <= $enddate)
		{
			$tmpArray =array();
			$tmpDate=$startdate+ 3600*24*$i;
			$tmpArray["date"] = date("Y-m-d",$tmpDate);
			$tmpArray["sid"] = $i;
			$tmpArray["leadname"]=$leaders[$i%$leaderCount]["name"];
			$tmpArray["staffname"]=$staffs[$i%$staffCount]["name"];
			$tmpArray["leadid"]=$leaders[$i%$leaderCount]["uid"];
			$tmpArray["staffid"]=$staffs[$i%$staffCount]["uid"];
			$i++;
			$j++;
			$tableArray[]=$tmpArray;
		}
		$this->schedulingmodel->updatescheduling($tableArray);

		$table2Array =$this->_getDate(
			$this->baseControllerData[$this->DATATYPE_CONTENT]["start_date"]
			,$this->baseControllerData[$this->DATATYPE_CONTENT]["end_date"]);
		$this->baseControllerData[$this->DATATYPE_CONTENT]["list"]=$table2Array;
		parent::index();
	}
	function _getDate($startdate=null,$enddate=null,$userid=-1)
	{
		$schedulingArray = $this->schedulingmodel->getlistscheduling($startdate
			,$enddate,$userid);
		$table2Array =Array();
		foreach($schedulingArray as $row)
		{
			$tmpArray =array();
			$tmpArray["date"] = $row["date"];
			$tmpArray["sid"] =  $row["sid"];
			$leaderrow = $this->usermodel->getuserbyid( $row["leadid"]);
			$staffrow = $this->usermodel->getuserbyid( $row["staffid"]);
			$tmpArray["leadname"]=$leaderrow["name"];
			$tmpArray["staffname"]=$staffrow["name"];
			$tmpArray["leadid"]= $row["leadid"];
			$tmpArray["staffid"]= $row["staffid"];
			$table2Array[]=$tmpArray;
		}
		return $table2Array;
	}
	function edit($id)
	{
		$this->baseControllerData[$this->DATATYPE_CONTENT]["type"] =4;
		$row = $this->schedulingmodel->getscheduling($id);
		$leaders = $this->usermodel->getuserbytype(1);
		$staffs = $this->usermodel->getuserbytype(0);
		$this->baseControllerData[$this->DATATYPE_CONTENT]["list"]=$row;
		$this->baseControllerData[$this->DATATYPE_CONTENT]["leaders"] =$leaders;
		$this->baseControllerData[$this->DATATYPE_CONTENT]["staffs"] =$staffs;
		
		parent::index();
	}
	function updatescheduling()
	{
		$num = $this->schedulingmodel->editscheduling($_POST["sid"],$_POST["leadid"],$_POST["staffid"]);
		
		if ($num>0)
		{
			$this->showerror("Update OK","",$this->MSG_DONE);
		}
		else
		{
			$this->showerror("Update failure");
		}
	}
}

?>